fix: clamp day-of-month when adding years/months (calendar overflow) - #41
Open
spokodev wants to merge 1 commit into
Open
fix: clamp day-of-month when adding years/months (calendar overflow)#41spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
end() (and thus toSeconds()) added months/years with the raw JS Date setters, so a day that does not exist in the target month overflowed into the next one: e.g. from Jan 31, P1M produced Mar 3 instead of Feb 28. This diverges from the @js-temporal/polyfill oracle the test suite validates against, which constrains the day to the target month. Add years/months on the 1st of the month, then clamp the day to the number of days in the resulting month before adding the day component.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
toSeconds/endoverflow the calendar when the start day does not exist in the target month.endadds years/months with the rawDatesetters:Setting the month on Jan 31 rolls over to Mar 3 instead of clamping to Feb 28. The same happens for years (Feb 29 + P1Y becomes Mar 1 instead of Feb 28).
The
@js-temporal/polyfillthat the suite validates against constrains the day to the target month:Fix: add years/months on the 1st of the month, then clamp the day to the number of days in the resulting month before adding the day component. Multi-unit and leap-year cases already handled by
Date(e.g.P2Mfrom Jan 31 stays Mar 31) are unchanged.Adds regression tests for the month, year, and month-then-day cases.